//
//  DeliveryRequireViewController.m
//  PrintPort
//
//  Created by 华谕 on 2017/11/22.
//  Copyright © 2017年 HUAYU. All rights reserved.
//

#import "DeliveryRequireViewController.h"

@interface DeliveryRequireViewController ()<UITextFieldDelegate,UITextViewDelegate>
{
    UIView *backView;
    UITextField *nameField;
    UITextField *gGField;
    UITextField *dWField;
    UITextField *dJField;
    UITextField *numField;
    UITextField *moneyField;
    UITextView *noteView;
}
@end

@implementation DeliveryRequireViewController
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(jianpanhuishou:) name:UIKeyboardWillHideNotification object:nil];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewTapped:)];
    tap1.cancelsTouchesInView = NO;
    [self.view addGestureRecognizer:tap1];
    [self creatUI];
}

-(void)viewTapped:(UITapGestureRecognizer*)tap1
{
    [self.view endEditing:YES];
}

- (void)creatUI
{
    self.navigationItem.title = @"递送需求";
    self.navigationController.navigationBar.translucent = NO;
    self.view.backgroundColor = BacksColor;
    backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    backView.backgroundColor = BacksColor;
    [self.view addSubview:backView];
    UIView *whiteView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 370*displaywidth)];
    whiteView.backgroundColor = [UIColor whiteColor];
    [backView addSubview:whiteView];
    NSArray *titAry = @[@"产品名称",@"规格",@"单位",@"单价",@"数量",@"金额",@"备注"];
    NSArray *pladerAry = @[@"请输入产品名称",@"请输入规格",@"请输入单位",@"请输入单价",@"请输入数量",@"请输入金额",@"有什么话想对商家说"];
    for (int i = 0; i < titAry.count; i++)
    {
        UILabel *titleLabel = [ZCControl createLabelWithFrame:CGRectMake(15*displaywidth, 45*displaywidth*i+5*displaywidth , 80*displaywidth, 35*displaywidth) Font:14.0 Text:titAry[i] Color:RGB(77, 77, 77)];
        [whiteView addSubview:titleLabel];
        UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 45*displaywidth*i-1*displaywidth, SCREEN_WIDTH, 1*displaywidth)];
        lineView.backgroundColor = LinesColor;
        [whiteView addSubview:lineView];
        if (i != titAry.count-1)
        {
            UITextField *textfield = [ZCControl createTextFieldWithFrame:CGRectMake(100*displaywidth, 45*displaywidth*i+5*displaywidth, SCREEN_WIDTH-120*displaywidth, 35*displaywidth) placeholder:pladerAry[i] passWord:NO leftImageView:nil rightImageView:nil Font:13.0];
            [textfield setValue:RGB(179, 179, 179) forKeyPath:@"_placeholderLabel.textColor"];
            textfield.textColor = RGB(77, 77, 77);
            textfield.textAlignment = NSTextAlignmentRight;
            textfield.delegate = self;
            [whiteView addSubview:textfield];
            if (i == 0)
            {
                nameField = textfield;
            }
            else if (i == 1)
            {
                gGField = textfield;
            }
            else if (i == 2)
            {
                dWField = textfield;
            }
            else if (i == 3)
            {
                dJField = textfield;
            }
            else if (i == 4)
            {
                textfield.keyboardType = UIKeyboardTypeNumberPad;
                numField = textfield;
            }
            else if (i == 5)
            {
                textfield.keyboardType = UIKeyboardTypeDecimalPad;
                moneyField = textfield;
            }
            
            
        }
        else
        {
            noteView = [[UITextView alloc] initWithFrame:CGRectMake(15*displaywidth, 45*displaywidth*i+40*displaywidth, SCREEN_WIDTH-30*displaywidth, 60*displaywidth)];
            noteView.delegate = self;
            noteView.text = pladerAry[i];
            noteView.textColor = RGB(179, 179, 179);
            [whiteView addSubview:noteView];
        }
        
    }
    UIButton *soureBtn = [ZCControl createButtonWithFrame:CGRectMake(60*displaywidth , 410*displaywidth, SCREEN_WIDTH-120*displaywidth, 45*displaywidth) ImageName:nil Target:self Action:@selector(soureAction) Title:@"确认" Font:15.0];
    [soureBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    soureBtn.backgroundColor = ThemeColor;
    [soureBtn.layer setMasksToBounds:YES];
    [soureBtn.layer setCornerRadius:5*displaywidth];
    [backView addSubview:soureBtn];
}


- (void)soureAction
{
    [self.view endEditing:YES];
    NSArray *textAry = @[nameField.text,gGField.text,dWField.text,dJField.text,numField.text,moneyField.text];
    NSArray *eqAry = @[@"",@"",@"",@"",@"",@""];
    NSArray *pladerAry = @[@"请输入产品名称",@"请输入规格",@"请输入单位",@"请输入单价",@"请输入数量",@"请输入金额"];
    for (int i = 0; i < textAry.count; i++)
    {
        if ([textAry[i] isEqualToString:eqAry[i]])
        {
            [LTools showMBProgressWithText:pladerAry[i] addToView:self.view];
            return;
        }
    }
    NSDictionary *dic = @{ @"productMoney": [NSString stringWithFormat:@"%.2lf",[moneyField.text floatValue]],
                           @"productName": nameField.text,
                           @"productNum": numField.text,
                           @"productSize": gGField.text,
                           @"productUnit": dWField.text,
                           @"productUnitPrice": dJField.text,
                           @"remark": noteView.text
                           };
    WEAKSELF;
    [HYBaseHandler requestDeliveryDemandParmers:dic Success:^(id obj)
     {
        if ([obj server_Getcodecheck])
        {
            [LTools showMBProgressWithText:@"递送成功" addToView:sharedDelegate.window];
            [weakSelf.navigationController popViewControllerAnimated:YES];
        }
        else
        {
            [LTools showMBProgressWithText:obj[@"msg"] addToView:weakSelf.view];
        }
    } failed:^(NSError *error) {
        
    }];
}






- (void)jianpanhuishou:(NSNotification *)notification
{
    backView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    if (textField == moneyField)
    {
        backView.frame = CGRectMake(0, -80*displaywidth, SCREEN_WIDTH, SCREEN_HEIGHT);

    }
    else
    {
        backView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    }

}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

//开始编辑
- (void)textViewDidBeginEditing:(UITextView *)textView
{
    textView.textColor = RGB(77, 77, 77);
    
    backView.frame = CGRectMake(0, -150*displaywidth, SCREEN_WIDTH, SCREEN_HEIGHT);
        
    
    if ([textView.text isEqualToString:@"有什么话想对商家说"])
    {
        textView.text= @"";
    }
}



//结束编辑
- (void)textViewDidEndEditing:(UITextView *)textView
{
    if ([textView.text isEqualToString:@""])
    {
        textView.text= @"有什么话想对商家说";
        textView.textColor = RGB(179, 179, 179);
    }
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
